package oa.hleast.xswl.service.pms.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import oa.hleast.xswl.mapper.pms.PmsMarketingFieldMapper;
import oa.hleast.xswl.pojo.domain.pms.PmsMarketingField;
import oa.hleast.xswl.pojo.domain.pms.PmsSpu;
import oa.hleast.xswl.pojo.dto.pms.SkuDTO;
import oa.hleast.xswl.service.pms.IPmsSkuService;
import oa.hleast.xswl.service.pms.IPmsSpuService;
import oa.hleast.xswl.service.pms.PmsMarketingFieldService;
import org.springframework.stereotype.Service;

/**
 * @author: 00209
 * @date: 2021-06-21 15:44:49
 * @description: 营销场管理表(PmsMarketingField)接口实现
 */
@Service("pmsMarketingFieldService")
@AllArgsConstructor
public class PmsMarketingFieldServiceImpl extends ServiceImpl<PmsMarketingFieldMapper, PmsMarketingField> implements PmsMarketingFieldService {

    private final IPmsSkuService pmsSkuService;
    private final IPmsSpuService pmsSpuService;

    @Override
    public boolean addBuyNumsAndMoney(Long relativeId) {
        LambdaUpdateWrapper<PmsMarketingField> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.setSql("cur_buy_nums=cur_buy_nums+1");
        lambdaUpdateWrapper.setSql("cur_buy_prices=cur_buy_prices+price");
        lambdaUpdateWrapper.eq(PmsMarketingField::getId, relativeId);
        boolean status = this.update(lambdaUpdateWrapper);
        PmsMarketingField pmsMarketingField = this.getById(relativeId);

        return status;
    }

    @Override
    public PmsMarketingField getDetailById(Long relativeId) {
        PmsMarketingField pmsMarketingField = this.getById(relativeId);
        SkuDTO skuDTO = pmsSkuService.getSkuById(pmsMarketingField.getSkuId());
        PmsSpu pmsSpu = pmsSpuService.getById(pmsMarketingField.getSpuId());
        pmsMarketingField.setSkuDTO(skuDTO);
        pmsMarketingField.setPmsSpu(pmsSpu);
        return pmsMarketingField;
    }

    @Override
    public PmsMarketingField getBySkuId(Long skuId) {
        PmsMarketingField pmsMarketingField = this.list(new LambdaQueryWrapper<PmsMarketingField>()
                .eq(PmsMarketingField::getSkuId, skuId)
                .last("limit 1")
        ).get(0);
        SkuDTO skuDTO = pmsSkuService.getSkuById(pmsMarketingField.getSkuId());
        PmsSpu pmsSpu = pmsSpuService.getById(pmsMarketingField.getSpuId());
        pmsMarketingField.setSkuDTO(skuDTO);
        pmsMarketingField.setPmsSpu(pmsSpu);
        return pmsMarketingField;
    }
}
